package com.jeesite.modules.kj.api.controller.team;


import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.request.TeamBuildRequestVo;
import com.jeesite.modules.kj.api.vo.response.IntegralHistoryResponseVo;
import com.jeesite.modules.kj.api.vo.response.MemberJobResponseVo;
import com.jeesite.modules.kj.api.vo.response.RankListResponseVo;
import com.jeesite.modules.kj.api.vo.response.TeamResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.api.wechatUtils.TokenUtils;
import com.jeesite.modules.kj.entity.*;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.*;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import com.jeesite.modules.kj.utils.TeamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "团队api", tags = {"团队API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/projectTeam")
public class ProjectTeamApiController {

    @Autowired
    KjMemberService kjMemberService;

    @Autowired
    KjContributeProjectService kjContributeProjectService;

    @Autowired
    KjProjectTeamService kjProjectTeamService;

    @Autowired
    KjTeamMemberService kjTeamMemberService;

    @Autowired
    KjMemberIntegralHistoryService kjMemberIntegralHistoryService;


    @ApiOperation(value = "创建团队", notes = "创建团队")
    @PostMapping(value = "build")
    @UserLoginToken
    public ResponseBean<String> build(@ApiIgnore @CurrentUser String memberId, TeamBuildRequestVo vo) throws FreezMemberException {
        KjProjectTeam team = vo.convertToEntity();
        KjContributeProject project = kjContributeProjectService.get(vo.getProjectId());
        KjMember member = KjMemberUtil.getCurrentMember(memberId);
        String teamId = TeamUtil.getMemberTeamId(member, project.getId());
        if(StringUtils.isNotBlank(teamId)){
            return new ResponseBean(Global.FALSE, "-30", "你已加入团队，无法再创建团队");
        }
        team.setKjMember(member);
        team.setKjContributeProject(project);
        kjProjectTeamService.save(team);
        KjTeamMember tm = new KjTeamMember();
        tm.setKjMember(member);
        tm.setTeamId(team.getId());
        tm.setProjectId(team.getProjectId());
        tm.setProjectName(team.getProjectName());
        tm.setTeamName(team.getName());
        kjTeamMemberService.save(tm);
        kjProjectTeamService.updateContributeMember(team);
        return new ResponseBean<>(team.getId());
    }

    @ApiOperation(value = "团队详情", notes = "团队详情")
    @PostMapping(value = "{id}")
    public ResponseBean<TeamResponseVo> detail(@PathVariable(name = "id") String id) {
        KjProjectTeam team = kjProjectTeamService.get(id);
        return new ResponseBean<>(new TeamResponseVo(team));
    }


    @ApiOperation(value = "团队捐赠详情", notes = "团队捐赠详情")
    @PostMapping(value = "{id}/contributes")
    public ResponseBean<List<IntegralHistoryResponseVo>> contributes(@PathVariable(name = "id") String id) {
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        where.setRelationId_like(id);
        List<KjMemberIntegralHistory> histories = kjMemberIntegralHistoryService.findList(where);
        List<IntegralHistoryResponseVo> result = ListUtils.newArrayList();
        for (KjMemberIntegralHistory h: histories
             ) {
            result.add(new IntegralHistoryResponseVo(h));
        }
        return new ResponseBean<>(result);
    }

    @ApiOperation(value = "团队用户捐赠排行", notes = "团队用户捐赠排行")
    @PostMapping(value = "{id}/rank")
    public ResponseBean<RankListResponseVo> rank(@PathVariable(name = "id") String id, HttpServletRequest httpServletRequest) throws FreezMemberException {
        String token = httpServletRequest.getHeader("Authorization");
        RankListResponseVo result = new RankListResponseVo();
        KjTeamMember tm = new KjTeamMember();
        tm.setTeamId(id);
        List<MemberIntegralRank> rankList = kjTeamMemberService.teamMemberRank(tm);
        // 执行认证
        if (StringUtils.isNotBlank(token)) {
            String memberId = TokenUtils.verify(token);
            KjMember currentMember = KjMemberUtil.getCurrentMember(memberId);
            tm.setKjMember(currentMember);
            MemberIntegralRank myRank = kjTeamMemberService.teamMemberRankNum(tm);
            result.setMyRank(myRank);
        }
        result.setRankList(rankList);
        return new ResponseBean<>(result);
    }

    @ApiOperation(value = "加入团队", notes = "加入团队")
    @PostMapping(value = "attention")
    @UserLoginToken
    public ResponseBean<Object> attention(@ApiIgnore @CurrentUser String memberId, String id) throws FreezMemberException {
        KjProjectTeam team = kjProjectTeamService.get(id);
        if(team.getAttentionNum() >= team.getLimitNum()){
            return new ResponseBean<>(Global.FALSE, "-20", "队伍已满员");
        }
        KjMember member = KjMemberUtil.getCurrentMember(memberId);
        KjTeamMember tm = new KjTeamMember();
        tm.setKjMember(member);
        tm.setProjectId(team.getKjContributeProject().getId());
        tm.setProjectName(team.getKjContributeProject().getName());
        tm.setTeamId(team.getId());
        tm.setTeamName(team.getName());
        kjTeamMemberService.save(tm);
        kjProjectTeamService.updateContributeMember(team);
        return new ResponseBean<>(new TeamResponseVo(team));
    }


    @ApiOperation(value = "我参加的团队捐赠", notes = "我参加的团队捐赠")
    @GetMapping(value = "my")
    @UserLoginToken
    public ResponseBean<List<MyTeam>> my(@ApiIgnore @CurrentUser String memberId, int year, int month) throws FreezMemberException {
        KjMember kjMember = KjMemberUtil.getCurrentMember(memberId);
        KjProjectTeam where = new KjProjectTeam();
        where.setKjMember(kjMember);
        if(year != 0 && month != 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month - 1, 1);
            where.setCreateDateStr(DateUtils.formatDate(calendar.getTime()));
        }
        List<MyTeam> teams = kjProjectTeamService.getProjectTeamByMemberId(where);
        return new ResponseBean<>(teams);
    }



}
